package com.yc.ui.dialog;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.GoodsIDao;
import com.yc.dao.GoodsDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Composite;

public class LiuyanDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text_datil;
	private String id = "0";
	private String r_id;
	
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public LiuyanDialog(Shell parent, int style, String r_id) {
		super(parent, style);
		setText("用户留言");
		this.r_id = r_id;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(753, 654);
		shell.setText("用户评价");
		
		SWTUtil.centerWindow(shell);
		text_datil = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		text_datil.setBounds(115, 344, 499, 184);
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(27, 167, 76, 20);
		lblNewLabel.setText("留言评价");
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setBounds(27, 10, 76, 20);
		lblNewLabel_1.setText("星级评价");
		
		Label lab_image = new Label(shell, SWT.NONE);
		lab_image.setImage(SWTResourceManager.getImage(LiuyanDialog.class, "/com/yc/images/R-C (1).jpg"));
		SWTUtil.imageSize(lab_image, 0);
		lab_image.setBounds(163, 37, 393, 40);
		
		Button but_0 = new Button(shell, SWT.RADIO);
		but_0.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lab_image.setImage(SWTResourceManager.getImage(LiuyanDialog.class, "/com/yc/images/R-C (1).jpg"));
				SWTUtil.imageSize(lab_image, 0);
				id = "0";
			}
		});
		but_0.setBounds(57, 112, 84, 20);
		but_0.setText("非常满意");
		
		Button but_1 = new Button(shell, SWT.RADIO);
		but_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lab_image.setImage(SWTResourceManager.getImage(LiuyanDialog.class, "/com/yc/images/R-C (1).jpg"));
				SWTUtil.imageSize(lab_image, 1);
				id = "1";
			}
		});
		but_1.setBounds(155, 112, 62, 20);
		but_1.setText("满意");
		
		Button btnRadioButton_2 = new Button(shell, SWT.RADIO);
		btnRadioButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lab_image.setImage(SWTResourceManager.getImage(LiuyanDialog.class, "/com/yc/images/R-C (1).jpg"));
				SWTUtil.imageSize(lab_image, 3);
				id = "3";
			}
		});
		btnRadioButton_2.setBounds(348, 112, 99, 20);
		btnRadioButton_2.setText("比较不满意");
		
		Button btnRadioButton_3 = new Button(shell, SWT.RADIO);
		btnRadioButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lab_image.setImage(SWTResourceManager.getImage(LiuyanDialog.class, "/com/yc/images/R-C (1).jpg"));
				SWTUtil.imageSize(lab_image, 4);
				id = "4";
			}
		});
		btnRadioButton_3.setBounds(472, 112, 84, 20);
		btnRadioButton_3.setText("不满意");
		
		Button btnRadioButton_4 = new Button(shell, SWT.RADIO);
		btnRadioButton_4.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lab_image.setImage(SWTResourceManager.getImage(LiuyanDialog.class, "/com/yc/images/R-C (1).jpg"));
				SWTUtil.imageSize(lab_image, 5);
				id = "5";
			}
		});
		btnRadioButton_4.setBounds(559, 112, 119, 20);
		btnRadioButton_4.setText("非常不满意");
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setBounds(73, 299, 76, 20);
		lblNewLabel_2.setText("评价留言：");
		
		Button btnRadioButton_5 = new Button(shell, SWT.RADIO);
		btnRadioButton_5.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lab_image.setImage(SWTResourceManager.getImage(LiuyanDialog.class, "/com/yc/images/R-C (1).jpg"));
				SWTUtil.imageSize(lab_image, 2);
				id = "2";
			}
		});
		btnRadioButton_5.setBounds(235, 112, 84, 20);
		btnRadioButton_5.setText("比较满意");
		
		Label lblNewLabel_3 = new Label(shell, SWT.NONE);
		lblNewLabel_3.setText("评价星级：");
		lblNewLabel_3.setBounds(73, 49, 84, 20);
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String datil = text_datil.getText().toString().trim();
				
				
				
				if(StringUtil.checkNull(id, r_id, datil)) {
					return;
				}
				GoodsIDao dao = new GoodsDao();
				System.out.println(r_id);
				System.out.println(id);
				System.out.println(datil);
				int i = dao.addappraise(r_id, id, datil, null);
				if(i > 0) {
					StringUtil.showMessageBox(shell, "提交成功！！！");
					shell.dispose();
				}else {
					StringUtil.showMessageBox(shell, "提交失败！！！");
				}
			}
		});
		btnNewButton.setBounds(186, 557, 98, 30);
		btnNewButton.setText("提交");
		
		Button btnNewButton_1 = new Button(shell, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnNewButton_1.setBounds(400, 557, 98, 30);
		btnNewButton_1.setText("取消");
	}
	
	protected byte[] getImageByte(String filePath) {
		byte [] bimage = null;
		//存入图片
		if(!StringUtil.checkNull(filePath)) {
			File file = new File(filePath);
			try(InputStream in = new FileInputStream(file)) {
				byte[] bt = new byte[(int) file.length()];
				while(in.read(bt) != -1) {
					//存入字节数组
					bimage = bt;
				}
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		return bimage;
	}
}
